<?php

require_once __DIR__ . '/../../../vendor/autoload.php';

use JLPay\Core\OrgConfig;
use JLPay\Core\DemoConfig;
use JLPay\Services\Trans\B2c\MicroPayPreAuthService;
use JLPay\Models\Trans\B2c\MicroPayPreAuthRequest;
use JLPay\Models\Trans\B2c\MicroPayPreAuthResponse;
use JLPay\Models\Trans\Dto\TerminalInfoDto;
use JLPay\Utils\Logger;
use JLPay\Utils\DemoRequestHandler;

/**
 * 交易 - B2C微支付预授权功能使用示例
 */

// =================== 工具函数 =====================
/**
 * 创建配置对象
 */
function createConfig(): OrgConfig
{
    return DemoConfig::createConfig();
}

// =================== 数据构建函数 =====================
/**
 * 构建微支付预授权请求
 */
function buildMicroPayPreAuthRequest(): MicroPayPreAuthRequest
{
    $request = new MicroPayPreAuthRequest();
    
    // 设置请求参数
    $request->setMchId("849584358120018");
    $request->setTermNo("60131234");
    $request->setPayType("wxpay");
    $request->setOutTradeNo("ld201911023465529");
    $request->setPaymentValidTime("10");
    $request->setGuaranteeExpiry("30");
    $request->setBody("充值中心-会员充值");
    $request->setAttach("商城—-易拉宝10个打印纸20箱");
    $request->setTotalFee("1");
    $request->setAuthCode("131208648101095993");
    $request->setRemark("该订单物流请走顺丰快递");
    $request->setOpUserId("001");
    $request->setOpShopId("GD_SZ_001");
    $request->setDeviceInfo("N50007A8890");
    $request->setMchCreateIp("123.12.12.123");
    $request->setTransAddress("深圳市南山区科技二路10号");
    $request->setAreaCode("4403");
    $request->setLongitude("116.078631");
    $request->setLatitude("39.71274");
    $request->setSyncAble(false);
    $request->setSubAppid("wxid_129752pfasf1671");
    
    // 设置终端信息
    $terminalInfo = new TerminalInfoDto();

    $request->setTerminalInfo($terminalInfo);
    
    return $request;
}

// =================== 示例函数 =====================
/**
 * 微支付预授权示例
 */
function microPayPreAuthDemo(): void
{
    $logger = Logger::getInstance();
    $logger->info("开始执行微支付预授权示例");
    
    // 创建配置对象和微支付服务
    $config = createConfig();
    $microPayService = new MicroPayPreAuthService($config);
    
    // 创建微支付预授权请求
    $request = buildMicroPayPreAuthRequest();
    
    // 发送请求 - 使用DemoRequestHandler
    $response = DemoRequestHandler::sendRequestAndHandleResponse(
        $microPayService, 
        $request, 
        "微支付预授权",
        function($service, $request) {
            return $service->microPayPreAuth($request);
        }
    );
    
    // 处理响应结果
    if ($response && $response->isSuccess()) {
        $logger->info("响应报文：" . json_encode($response->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }
    
    $logger->info("微支付预授权示例执行完毕");
}

// =================== 主程序 =====================
$logger = Logger::getInstance();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 微支付预授权示例 开始 =====");
$logger->info("=" . str_repeat("=", 50));

microPayPreAuthDemo();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 微支付预授权示例执行完毕 =====");
$logger->info("=" . str_repeat("=", 50)); 